/*
 * @Author       : wang chao
 * @Date         : 2023-05-01 07:18:34
 * @LastEditors  : wang chao
 * @LastEditTime : 2023-05-17 09:45:43
 * @FilePath     : lb_virtualIO.c
 * @Description  :
 * Copyright 2023 BingShan, All Rights Reserved.
 */
#include "stdio.h"
#include "string.h"
#include "board.h"
#include "lite_mb.h"
#include "lb_virtualIO.h"

#define HOLD_REG_MAX 125
static uint16_t hold_regs[HOLD_REG_MAX] = {0};

// 上传参数
extern ADS_Param   ADS_AIS[10];
extern rt_int32_t  soc_percent;
extern rt_int16_t  system_total_power;
extern rt_uint16_t modbus_get_phy_value(rt_uint16_t reg);
extern rt_uint16_t slave_holding_register_value[MB_READ_NUMBER];

int lbo_read_hold_regs(uint16_t addr, uint16_t num, uint16_t* buffer)
{
    uint16_t io_start = 0, io_end = 0, index = 0, temp = 0;

    for (index = 0; index < HOLD_REG_MAX; index++)
    {
        // 动态更新参数
        if (index < addr)
        {
            continue;
        }
        if (index > (addr + num))
        {
            break;
        }

        if (index >= 0 && index < 10)
        {
            // 上传电压板10路电压
            hold_regs[index] = (uint16_t)(ADS_AIS[index].vol_value);
        }
        else if (index >= 10 && index < 26)
        {
            // 上传4-20mA传感器参数
            temp = index - 10;
            if (temp == 4)
            {
                hold_regs[index] = (uint16_t)(system_total_power);
            }
            else if (temp == 15)
            {
                hold_regs[index] = (uint16_t)(soc_percent);
            }
            else
            {
                hold_regs[index] = (uint16_t)(modbus_get_phy_value(temp));
            }
        }
        else if (index >= 26 && index < 52)
        {
            temp             = index - 26;
            hold_regs[index] = slave_holding_register_value[temp];
        }
        else
        {
            hold_regs[index] = 0;
        }
    }

    if (addr > 124)
    {
        addr = addr - 125;
    }
    index = 0;
    if ((0 <= addr) && (addr < HOLD_REG_MAX))
    {
        io_start = addr;
        if ((addr + num) > (HOLD_REG_MAX))
        {
            io_end = HOLD_REG_MAX;
        }
        else
        {
            io_end = addr + num;
        }
#if (SMALL_MODBUS_USING_MSG_DEBUG)
        rt_kprintf("io_start %d io_end %d num %d", io_start, io_end, num);
#endif
        while (io_start < io_end)
        {
            buffer[index] = hold_regs[io_start];
            io_start++;
            index++;
        }
    }
    return index;
}

int lbo_write_hold_regs(uint16_t addr, uint16_t num, uint16_t* buffer)
{
    //    uint16_t value = 0;
    // if ((0 <= addr) && (addr < HOLD_REG_MAX))
    // {
    //     if (addr >= 0 && addr < 25)
    //     {
    //         value = lite_aio_get_val(buffer, 0);
    //         lbo_alarm_value_process(addr, value);
    //     }
    //     else if (addr >= 25 && addr < 32)
    //     {
    //         // 制冷器启动温度
    //         value = lite_aio_get_val(buffer, 0);
    //         lbo_ctrl_value_process(addr, value);
    //     }
    // }
    return num;
}
